<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Code extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Codemodel','m_code');
	}

	public function index(){
		$data = parent::index();
		$this->load->view('code',$data);
	}

	public function getCodeList(){
		$assist['where'] = getFilter($this->input->post(),Codemodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Codemodel::$likeField);
		$assist['pagination'] = getPagination($this->input->post());
		list($list,$count) = $this->m_code->getCodeListSection($assist);

		$users = extractColumn($list,'user_id');
		$users = $this->m_user->getUserById($users);
		$users = reindexArray($users,'user_id');
		foreach ($list as &$record) {
			$record['user_name'] = '';
			if(isset($users[$record['user_id']])) $record['user_name'] = $users[$record['user_id']]['user_name'];
			//$record['code_desc'] = htmlspecialchars($record['code_desc']);
			//$record['code_desc'] = nl2br($record['code_desc']);
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createCode(){
		$this->validate->setRule('code_name','string');
		$this->validate->setRule('code_name','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_desc','string');
		$this->validate->setRule('code_desc','optional');
		$this->validate->setRule('code_desc','notnull',false);
		$this->validate->setRule('code_desc','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_content_js','string');
		$this->validate->setRule('code_content_js','optional');
		$this->validate->setRule('code_content_js','notnull',false);
		$this->validate->setRule('code_content_js','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_content_css','string');
		$this->validate->setRule('code_content_css','optional');
		$this->validate->setRule('code_content_css','notnull',false);
		$this->validate->setRule('code_content_css','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!$this->m_code->checkNameAvailable($info['code_name'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['code_status'] = STATUS_DISABLE;
		$info['code_time_create'] = NOW;
		$code_id = $this->m_code->createCode($info);

		$old = umask(0);
		@mkdir(CODE_IMAGE_PATH.$code_id,0777,true);
		umask($old);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editCode(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_name','string');
		$this->validate->setRule('code_name','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_desc','string');
		$this->validate->setRule('code_desc','optional');
		$this->validate->setRule('code_desc','notnull',false);
		$this->validate->setRule('code_desc','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_content_js','string');
		$this->validate->setRule('code_content_js','optional');
		$this->validate->setRule('code_content_js','notnull',false);
		$this->validate->setRule('code_content_js','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_content_css','string');
		$this->validate->setRule('code_content_css','optional');
		$this->validate->setRule('code_content_css','notnull',false);
		$this->validate->setRule('code_content_css','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_time_lastmodified','string');
		$this->validate->setRule('code_time_lastmodified','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$code = $this->m_code->getCode($info['code_id']);
		$time = NOW;
		if(empty($code) || $code['code_time_lastmodified'] != $info['code_time_lastmodified']){
			$this->response->setDetail($this->lang->line('error_edited'));
			$this->response->responseExit();
		}
		if(!$this->m_code->checkNameAvailable($info['code_name'],$info['code_id'])){
			$this->response->setDetail($this->lang->line('error_code_name_used'));
			$this->response->responseExit();
		}
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['code_time_lastmodified'] = NOW;
		$this->m_code->editCode($info);
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('code_time_lastmodified'=>$time));
		$this->response->responseExit();
	}

	public function deleteCode(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$info['code_status'] = STATUS_DELETE;
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$this->m_code->editCode($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function enableCode(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$info['code_status'] = STATUS_ACTIVE;
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$this->m_code->editCode($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function disableCode(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$info['code_status'] = STATUS_DISABLE;
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$this->m_code->editCode($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}


	public function editCodeSort(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('code_sort','integer');
		$this->validate->setRule('code_sort','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		$this->load->database();
		$info['code_sort'] = $info['code_sort'];
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$this->m_code->editCode($info);
		$this->db->close();

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	// Define the custom sort function
	static function custom_sort($a,$b) {
	        return $a['code_image_time_lastmodified']<$b['code_image_time_lastmodified'];
	}

	public function getCodeImageList(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));

		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		//$file_pos = UPLOAD_PATH.$project['project_code'].'/'.$info['project_file']['name'][$i];
		$info = $this->validate->getResult();
	
		$images = @scandir(CODE_IMAGE_PATH.$info['code_id']);
		$list = array();
		if($images) {
			foreach ($images as $img) {
				$file = CODE_IMAGE_PATH.$info['code_id'].'/'.$img;
				if(is_dir($file)) continue;

				$size = getimagesize($file);
				
				
				$list[] = array(
					'code_image_name' => $img,
					'code_image_path' => $file,
					'code_image_html' => '<span style="color:#800;">background:transparent url('.str_replace(dirname(BASEPATH),'',CODE_IMAGE_PATH).$info['code_id'].'/'.$img.') no-repeat 0 0;</span>',
					'code_image_time_lastmodified' => date("Y-m-d H:i:s", filemtime($file)),
					'code_image_url' => CODE_IMAGE_URL.$info['code_id'].'/'.$img,
					'code_image_size' => round((filesize($file) / 1024), 2),
				);

			}
		}
		

		usort($list, array("Code", "custom_sort"));
		
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function addCodeImage(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		
		
		$file = $_FILES['code_image'];

		if(!file_exists(CODE_IMAGE_PATH.$info['code_id'])) {
			$old = umask(0);
			@mkdir(CODE_IMAGE_PATH.$info['code_id'],0777,true);
			umask($old);
		}

		$file_pos = CODE_IMAGE_PATH.$info['code_id'].'/'.$file['name'][0];
		move_uploaded_file($file['tmp_name'][0],$file_pos);

		$this->response->setSuccess();
		$detail = $this->response->getDetail();
		if(!validate($detail)) $this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function removeCodeImage(){
		$this->validate->setRule('code_image_path','string');
		$this->validate->setRule('code_image_path','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		
		if(file_exists($info['code_image_path'])){
			unlink($info['code_image_path']);
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file code.php */
/* Location: ./application/controllers/code.php */